package com.dengcj.util;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @author dengchunjie
 * @Description: 文件工具类
 * @date 2022/1/12 09:49
 */
public class CjFileUtil {

    /**
     * 将文件转换为Base64字符串
     * @param filePath 文件绝对路径
     * @return
     */
    public static String encryptToBase64(String filePath) {
        return encryptToBase64(filePath, false, "");
    }

    /**
     * 将文件转换为Base64字符串
     * @param filePath 文件绝对路径
     * @param needPrefix 是否需要前缀
     * @return
     */
    public static String encryptToBase64(String filePath, boolean needPrefix) {
        String filename = "";
        if(needPrefix) {
            filename = filePath.substring(filePath.lastIndexOf("."));
        }
        return encryptToBase64(filename, needPrefix, filePath);
    }

    /**
     * 将文件转换为Base64字符串
     * @param filePath 文件绝对路径
     * @param needPrefix 是否需要前缀
     * @param filename 文件名
     * @return
     */
    public static String encryptToBase64(String filePath, boolean needPrefix, String filename) {
        if (CjUtil.isBlank(filePath)) {
            return "";
        }

        try {
            byte[] b = Files.readAllBytes(Paths.get(filePath));

            String prefix = "";
            if(needPrefix){
                // 获取文件后缀名字
                String  suffix = filename.substring(filename.lastIndexOf(".")+1);

                prefix = getFileBase64StrPrefix(suffix);
            }

            return prefix + Base64.getEncoder().encodeToString(b);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }

    /**
     * 根据文件后缀名转换文件base64字符串的前缀
     * @param fileSuffix
     * @return
     */
    public static String getFileBase64StrPrefix(String fileSuffix){
        Map<String, String> base64FormatMap = new HashMap<>();
        base64FormatMap.put("png","image/png");
        base64FormatMap.put("jpg","image/jpeg");
        base64FormatMap.put("jpeg","image/jpeg");
        base64FormatMap.put("gif","image/gif");
        base64FormatMap.put("svg","image/svg+xml");
        base64FormatMap.put("ico","image/x-icon");
        base64FormatMap.put("bmp","image/bmp");

        base64FormatMap.put("pdf","application/pdf");

        base64FormatMap.put("ofd","application/ofd");

        base64FormatMap.put("txt","text/plain");

        base64FormatMap.put("doc","application/msword");
        base64FormatMap.put("docx","application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        base64FormatMap.put("xls","application/vnd.ms-excel");
        base64FormatMap.put("xlsx","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        base64FormatMap.put("ppt","application/vnd.ms-powerpoint");
        base64FormatMap.put("pptx","application/vnd.openxmlformats-officedocument.presentationml.presentation");

        String prefix = "data:application/octet-stream;base64,";

        String format = base64FormatMap.get(fileSuffix);
        if(CjUtil.isNotBlank(format)) {
            prefix = "data:"+format+";base64,";
        }

        return prefix;
    }

    /**
     * 将base64字符串转为文件
     * @param fileBase64Str
     * @param filePath
     * @return
     */
    public static boolean base642File(String fileBase64Str, String filePath){
        if (CjUtil.isBlank(fileBase64Str) || CjUtil.isBlank(filePath)) {
            return false;
        }
        try {
            Files.write(Paths.get(filePath), Base64.getDecoder().decode(fileBase64Str), StandardOpenOption.CREATE);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }
}
